package com.meeting.master.service.base;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ReflectUtil;

import com.meeting.master.manager.bo.*;
import com.meeting.master.manager.bo.RoomInfoBO;
import com.meeting.master.persist.dos.RoomInfo;
import com.meeting.master.service.base.BaseRoomInfoBOService.CreateRoomInfoBoResult;
import com.meeting.master.service.bto.CreateRoomInfoBto;
import com.vs.bo.AddedBto;
import com.vs.bo.BaseBoResult;
import com.vs.bo.UnmodifiedBto;
import com.vs.bo.UpdatedBto;
import com.vs.code.AutoGenerated;
import com.vs.code.DoNotModify;
import com.vs.ox.common.exception.IgnoredException;
import com.vs.sqlmapper.core.PgIdGenerator;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Component;

import java.util.Set;

import javax.annotation.Resource;

@DoNotModify
@Slf4j
@Component
@AutoGenerated(locked = true, uuid = "18b2374d-14e8-3a9f-a9b4-322c8d432861")
public class BaseRoomInfoBOService {
    @AutoGenerated(locked = true)
    @Resource(name = "pgIdGenerator")
    private PgIdGenerator idGenerator;

    /** 数据库创建一行, 数据库创建一行 */
    @AutoGenerated(locked = true)
    private RoomInfoBO createCreateRoomInfo(
            BaseRoomInfoBOService.CreateRoomInfoBoResult boResult,
            CreateRoomInfoBto createRoomInfoBto) {
        RoomInfoBO roomInfoBO = null;
        boolean found = false;
        boolean allNull = false;
        boolean pkMatched = false;
        boolean pkExist = false;
        String matchedUkName = "";
        if (roomInfoBO != null) {
            if (pkMatched) {
                log.error("主键冲突, id:{}的记录在数据库表:{}中已经存在!", roomInfoBO.getId(), "master_room_info");
                throw new IgnoredException(400, "会议室已存在");
            } else {
                log.error(
                        "唯一键UK{}和数据库表:'{}'中id为:{}的记录冲突",
                        matchedUkName,
                        "master_room_info",
                        roomInfoBO.getId());
                throw new IgnoredException(400, "会议室已存在");
            }
        } else {
            roomInfoBO = new RoomInfoBO();
            if (pkExist) {
                roomInfoBO.setId(this.idGenerator.allocateId("master_room_info"));
            } else {
                roomInfoBO.setId(this.idGenerator.allocateId("master_room_info"));
            }
            if (CollectionUtil.contains(
                    (Set<String>)
                            ReflectUtil.getFieldValue(createRoomInfoBto, "__$validPropertySet"),
                    "name")) {
                roomInfoBO.setName(createRoomInfoBto.getName());
            }
            if (CollectionUtil.contains(
                    (Set<String>)
                            ReflectUtil.getFieldValue(createRoomInfoBto, "__$validPropertySet"),
                    "type")) {
                roomInfoBO.setType(createRoomInfoBto.getType());
            }
            if (CollectionUtil.contains(
                    (Set<String>)
                            ReflectUtil.getFieldValue(createRoomInfoBto, "__$validPropertySet"),
                    "seatNum")) {
                roomInfoBO.setSeatNum(createRoomInfoBto.getSeatNum());
            }
            if (CollectionUtil.contains(
                    (Set<String>)
                            ReflectUtil.getFieldValue(createRoomInfoBto, "__$validPropertySet"),
                    "address")) {
                roomInfoBO.setAddress(createRoomInfoBto.getAddress());
            }
            if (CollectionUtil.contains(
                    (Set<String>)
                            ReflectUtil.getFieldValue(createRoomInfoBto, "__$validPropertySet"),
                    "floor")) {
                roomInfoBO.setFloor(createRoomInfoBto.getFloor());
            }
            if (CollectionUtil.contains(
                    (Set<String>)
                            ReflectUtil.getFieldValue(createRoomInfoBto, "__$validPropertySet"),
                    "status")) {
                roomInfoBO.setStatus(createRoomInfoBto.getStatus());
            }
            AddedBto addedBto = new AddedBto();
            addedBto.setBto(createRoomInfoBto);
            addedBto.setBo(roomInfoBO);
            boResult.getAddBtoList().add(addedBto);
        }
        return roomInfoBO;
    }

    /** 创建会议室 */
    @AutoGenerated(locked = true)
    protected CreateRoomInfoBoResult createRoomInfoBase(CreateRoomInfoBto createRoomInfoBto) {
        if (createRoomInfoBto == null) {
            throw new IgnoredException(400, "bto 不能为null!");
        }
        CreateRoomInfoBoResult boResult = new CreateRoomInfoBoResult();
        RoomInfoBO roomInfoBO = createCreateRoomInfo(boResult, createRoomInfoBto);
        boResult.setRootBo(roomInfoBO);
        return boResult;
    }

    public static class CreateRoomInfoBoResult extends BaseBoResult {

        @AutoGenerated(locked = true)
        public RoomInfoBO getRootBo() {
            return (RoomInfoBO) this.rootBo;
        }

        @AutoGenerated(locked = true)
        public AddedBto<CreateRoomInfoBto, RoomInfoBO> getCreatedBto(
                CreateRoomInfoBto createRoomInfoBto) {
            return this.getAddedResult(createRoomInfoBto);
        }

        @AutoGenerated(locked = true)
        public RoomInfo getDeleted_RoomInfo() {
            return (RoomInfo) CollectionUtil.getFirst(this.getDeletedEntityList(RoomInfo.class));
        }

        @AutoGenerated(locked = true)
        public UpdatedBto<CreateRoomInfoBto, RoomInfo, RoomInfoBO> getUpdatedBto(
                CreateRoomInfoBto createRoomInfoBto) {
            return super.getUpdatedResult(createRoomInfoBto);
        }

        @AutoGenerated(locked = true)
        public UnmodifiedBto<CreateRoomInfoBto, RoomInfoBO> getUnmodifiedBto(
                CreateRoomInfoBto createRoomInfoBto) {
            return super.getUnmodifiedResult(createRoomInfoBto);
        }
    }
}
